package com.shop.item.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.shop.common.entity.item.ItemBase;
import com.shop.common.entity.item.ItemDetails;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.item.ItemBaseInput;
import com.shop.common.model.param.item.ItemDetailsInput;
import com.shop.common.model.param.item.ItemEditHisInput;
import com.shop.common.model.result.item.ItemBaseOutput;
import com.shop.common.model.result.item.ItemDetailsOutput;
import com.shop.common.model.result.item.ItemEditHisOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.DateUtil;
import com.shop.common.util.annotation.FieldName;
import com.shop.common.util.state.DataStatus;
import com.shop.common.util.state.ItemType;
import com.shop.item.atom.atomsvc.IItemAtomService;
import com.shop.item.atom.atomsvc.IItemEditAtomService;
import com.shop.item.atom.atomsvc.IItemExtAtomService;
import com.shop.item.api.service.IItemEditService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemEditService extends BaseService implements IItemEditService {
    static final Logger logger = LoggerFactory.getLogger(ItemEditService.class);

    @Autowired
    IItemAtomService itemApiService;
    @Autowired
    IItemEditAtomService itemEditApiService;
    @Autowired
    IItemExtAtomService itemExtApiService;

    @Override
    public ServiceBean<String> updateItemBase(InputBean<ItemBaseInput> inputBean) {
        ItemBaseInput itemBaseInput = new ItemBaseInput();
        itemBaseInput.setItemId(inputBean.getParam().getItemId());
        ServiceBean<ItemBaseOutput> serviceBean = itemApiService.selectItemBase(itemBaseInput);
        if (!serviceBean.isSuccess()) {
            return resultFailed("商品不存在，更新失败");
        }
        itemBaseInput = inputBean.getParam();
        ItemBase oldItemBase = serviceBean.getData();
        Field[] fields = ItemBase.class.getDeclaredFields();
        Map<String, String> modifyMap = new HashMap<>();
        Boolean upFlag = false;
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Annotation annotation = field.getAnnotation(FieldName.class);
                if (annotation != null) {
                    String field1 = field.get(oldItemBase) + "";
                    String field2 = field.get(itemBaseInput) + "";
                    String fieldName = ((FieldName) annotation).value();
                    if (!field1.equals(field2)) {
                        modifyMap.put(fieldName, field1 + "->" + field2);
                        upFlag = true;
                    }

                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return resultFailed();
        }
        if (!upFlag) {
            return resultFailed("没有进行任何修改");
        }
        itemBaseInput.setModifyTime(DateUtil.newNowDate());
        ServiceBean serviceBean1 = itemApiService.updateItemBase(itemBaseInput);
        if (!serviceBean1.isSuccess()) {
            return resultFailed();
        }
        itemBaseInput = new ItemBaseInput();
        itemBaseInput.setItemId(oldItemBase.getItemId());
        serviceBean = itemApiService.selectItemBase(itemBaseInput);
        ItemBase newItemBase = serviceBean.getData();

        ItemEditHisInput editHisInput = new ItemEditHisInput();
        editHisInput.setItemId(itemBaseInput.getItemId());
        editHisInput.setAfterData(JSON.toJSONString(newItemBase));
        editHisInput.setFormerData(JSON.toJSONString(oldItemBase));
        editHisInput.setModifyData(JSON.toJSONString(modifyMap));
        editHisInput.setCreateTime(DateUtil.newNowDate());
        editHisInput.setItemType(ItemType.BASE.value());
        editHisInput.setDataStatus(DataStatus.USE.value());
        editHisInput.setModifyReason("");

        ServiceBean serviceBean2 = itemEditApiService.insertItemEditHis(editHisInput);
        if (serviceBean2.isSuccess()) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> updateItemDetails(InputBean<ItemDetailsInput> inputBean) {
        ItemDetailsInput itemDetailsInput = new ItemDetailsInput();
        itemDetailsInput.setId(inputBean.getParam().getId());
        ServiceBean<ItemDetailsOutput> serviceBean = itemApiService.selectItemDetails(itemDetailsInput);
        if (!serviceBean.isSuccess()) {
            return resultFailed("商品不存在，更新失败");
        }
        itemDetailsInput = inputBean.getParam();
        ItemDetails oldItem = serviceBean.getData();
        Field[] fields = ItemDetails.class.getDeclaredFields();
        Map<String, String> modifyMap = new HashMap<>();
        //获取商品修改的字段
        Boolean upFlag = false;
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Annotation annotation = field.getAnnotation(FieldName.class);
                if (annotation != null) {
                    String field1 = field.get(oldItem) + "";
                    String field2 = field.get(itemDetailsInput) + "";
                    String fieldName = ((FieldName) annotation).value();
                    if (!field1.equals(field2)) {
                        modifyMap.put(fieldName, field1 + "->" + field2);
                        upFlag = true;
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return resultFailed("更新失败");
        }
        if (!upFlag) {
            return resultFailed("没有进行任何修改");
        }

        ServiceBean serviceBean1 = itemApiService.updateItemDetails(itemDetailsInput);
        if (!serviceBean1.isSuccess()) {
            return resultFailed();
        }
        itemDetailsInput = new ItemDetailsInput();
        itemDetailsInput.setId(oldItem.getId());
        serviceBean = itemApiService.selectItemDetails(itemDetailsInput);
        ItemDetails newItemDetails = serviceBean.getData();

        ItemEditHisInput editHisInput = new ItemEditHisInput();
        editHisInput.setItemId(oldItem.getItemId());
        editHisInput.setAfterData(JSON.toJSONString(newItemDetails));
        editHisInput.setFormerData(JSON.toJSONString(oldItem));
        editHisInput.setModifyData(JSON.toJSONString(modifyMap));
        editHisInput.setCreateTime(DateUtil.newNowDate());
        editHisInput.setItemType(ItemType.DETAILS.value());
        editHisInput.setDataStatus(DataStatus.USE.value());
        editHisInput.setModifyReason("");
        ServiceBean serviceBean2 = itemEditApiService.insertItemEditHis(editHisInput);
        if (serviceBean2.isSuccess()) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<ItemEditHisOutput>> selectItemEditHisList(InputBean<ItemEditHisInput> inputBean) {
        return null;
    }

    @Override
    public ServiceBean<ItemEditHisOutput> selectItemEditHis(InputBean<ItemEditHisInput> inputBean) {
        return null;
    }
}
